from django import forms

from accounts.models.server_auth_model import UsersServerAuth
from assets.models import ProjectSwan
from .models import AuthGroup, UserAuthCmdb


class RoleForm(forms.ModelForm):
    enable = forms.TypedChoiceField(
        coerce=lambda x: x == 'True',
        choices=((True, '启用'), (False, '禁用')),
        required=True, initial=True,
        widget=forms.RadioSelect,
        label="是否启用"
    )

    class Meta:
        model = AuthGroup
        fields = ('group_name', 'explanation', 'enable')


class RoleUserForm(forms.ModelForm):
    """ 角色用户关联表单 """

    class Meta:
        model = AuthGroup
        fields = ["group_user"]


class RolePermissionLinkForm(forms.ModelForm):
    """
    角色与权限关联表单
    setup_system
    upload_system
    salt_keys = m

    """

    class Meta:
        model = UserAuthCmdb

        fields = ["select_host", "add_host", "bat_add_host", "edit_host", "update_host", "delete_host", "add_user",
                  "edit_user", "edit_pass", "delete_user", "add_department", "group_name", "add_idc", "edit_idc",
                  "del_idc", "setup_system", "upload_system", "salt_keys", "auth_log", "project_auth", "add_project",
                  "edit_project", "delete_project", "add_line_auth", "select_idc", "auth_project", "auth_highstate",
                  "cmdb_log", "server_audit"
                  ]


class PushPermissionMemberMgrForm(forms.ModelForm):
    """ 发布项目权限成员管理表单"""

    class Meta:
        model = ProjectSwan
        fields=['push_user']






class HostPermissionCreateForm(forms.ModelForm):
    """" 用户分配服务器权限记录? """


    class Meta:
        model = UsersServerAuth
        fields = [
            "server_ip",
            "user_name",
            "auth_weights",
        ]
        """
        server_ip = models.IPAddressField(blank=True, null=True, verbose_name=u'服务器')
        user_name = models.CharField(max_length=20, blank=True, null=True, verbose_name=u'用户名')
        auth_weights = models.CharField(max_length=32, blank=True, null=True, default=u"普通用户", choices=auth_id, verbose_name=u'权限')
        datetime = models.DateTimeField(auto_now=True)

        """